package com.tiyende.credit.loop.ui.presenter

import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonPresenter
import com.tiyende.credit.loop.configs.TiyendeApiUrl
import com.tiyende.credit.loop.configs.TiyendeConfigs
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.ui.contract.IViewContract
import com.tiyende.credit.loop.utils.helper.TiyendeProcessHelper
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 11:59
 * @Annotation：进件 Presenter 实现
 */
class TiyendeAuthenticationStepsPresenter(private val iView: IViewContract.ITiyendeAuthenticationView): CommonPresenter(iView) {

    /**
     * 当前步骤的名称
     */
    private var eventStepTitle = ""
    fun getProcessEventTitle(): String{
        return eventStepTitle
    }

    /**
     * 根据Step类型显示不同的Page
     */
    fun loadProcessPageAction(){
        //移除所有步骤
        iView.removeTiyendeAuthenticationStepAllViews()

        val allStepTitles = TiyendeProcessHelper.queryTiyendeAuthenticationAllStepTitleList()
        //当前进行的步骤
        val curtIndex = TiyendeProcessHelper.queryTiyendeAuthenticationStepIndex()
        val allSteps = TiyendeProcessHelper.queryTiyendeAuthenticationAllStepList()
        if (curtIndex == allSteps.size){//所有步骤已完成
            iView.showTiyendeAuthenticationStepCompleteViews()
            return
        }
        val showTitle = allStepTitles[curtIndex]
        iView.showTiyendeAuthenticationStepProgress(allSteps.size,curtIndex)
        iView.showTiyendeAuthenticationCurrentStep(getProcessTypeDrawableByTitle(allSteps[curtIndex]),"${curtIndex+1}.$showTitle")

        val processData = TiyendeProcessHelper.queryTiyendeAuthenticationData()
        eventStepTitle = allSteps[curtIndex]
        //是否是坦桑地区,需要根据语言显示
        if (TiyendeConfigs.isTanz()){
//            val stepTitleItem = JSONObject(stepTitle)
//            val curtLocale = Languages.getAppLanguage(iView.getAct())
//            stepTitle = when(curtLocale.language){
//                LocaleContract.getSwahiliLocale().language -> {//坦桑
//                    stepTitleItem.optString("sw")
//                }
//                else -> {
//                    stepTitleItem.optString("en")
//                }
//            }
        }
        //获取对应的key
        val curtKey = allSteps[curtIndex]
        processData?.let {
            //todo Contact 类型
            if (it.has(curtKey)){
                val stepData = it.optJSONObject(curtKey)
                stepData?.put("showTitle",showTitle)
                stepData?.put("stepTitle",eventStepTitle)
                if (stepData != null && stepData.has("isContact") && stepData.optString("isContact") == "YES"){
                    iView.showTiyendeAuthenticationStepContactView(stepData)
                    return
                }
            }
            //todo Account账户样式（法语地区绑定账号）
            if (curtKey.contains("account_info") && TiyendeConfigs.isFrench()){
                val stepData = JSONObject()
                stepData.put("showTitle",showTitle)
                stepData.put("stepTitle",eventStepTitle)
                iView.showTiyendeAuthenticationStepBindWalletView(stepData)
                return
            }
            //常规通用样式
            if (it.has(curtKey)){
                val stepData = it.optJSONObject(curtKey)
                if (stepData != null) {
                    stepData.put("showTitle",showTitle)
                    stepData.put("stepTitle",eventStepTitle)
                    iView.showTiyendeAuthenticationStepCommonView(stepData)
                }
            }
        }
    }

    /**
     * 提交数据
     */
    fun onAuthenticationStepSubmitAction(params: JSONObject){
        iView.showTiyendeLoading()
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showTiyendeAuthenticationStepSubmitSuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 根据进件step名称获取对应的icon
     */
    private fun getProcessTypeDrawableByTitle(stepTitle: String): Int{
        when(stepTitle){
            "personal_info" -> {
                return R.mipmap.tiyende_activity_authentication_steps_personal_info_icon
            }
            "work_info" -> {
                return R.mipmap.tiyende_activity_authentication_steps_work_info_icon
            }
            "contact_info" -> {
                return R.mipmap.tiyende_activity_authentication_steps_contact_info_icon
            }
            "account_info" -> {
                return R.mipmap.tiyende_activity_authentication_steps_account_info_icon
            }
            "sefie_info" -> {
                return R.mipmap.tiyende_activity_authentication_steps_sefie_info_icon
            }
            "ocr_info" -> {
                return R.mipmap.tiyende_activity_authentication_steps_ocr_info_icon
            }
        }
        return R.mipmap.tiyende_activity_authentication_steps_personal_info_icon
    }
}